package com.vitabuybuy.tv.ui.auth

import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityRegisterBinding
import com.library.common.base.BaseActivity
import com.library.common.ext.setOnclickNoRepeat
import com.library.common.toast.XToast
import com.vitabuybuy.tv.ui.auth.viewmodel.AuthViewModel
import com.vitabuybuy.tv.utils.StringUtil
import kotlinx.android.synthetic.main.activity_set_password.*

class SetPasswordActivity : BaseActivity<AuthViewModel, ActivityRegisterBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_set_password

    private var phone: String? = ""
    private var countryCode: String? = ""

    override fun onGetBundle(bundle: Bundle?) {
        phone = bundle?.getString("phone")
        countryCode = bundle?.getString("countryCode")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.set_password))

    }

    override fun eventHandling() {
        setOnclickNoRepeat(ivEye, ivEyeAgain, btnNext){v->
            when (v) {
                ivEye -> {
                    if (etPassword.transformationMethod == HideReturnsTransformationMethod.getInstance()) {
                        etPassword.transformationMethod = PasswordTransformationMethod.getInstance()
                        ivEye.setImageResource(R.drawable.icon_login_eyes_n)
                    } else {
                        etPassword.transformationMethod = HideReturnsTransformationMethod.getInstance()
                        ivEye.setImageResource(R.drawable.icon_login_eyes)
                    }
                }
                ivEyeAgain -> {
                    if (etPasswordAgain.transformationMethod == HideReturnsTransformationMethod.getInstance()) {
                        etPasswordAgain.transformationMethod =
                            PasswordTransformationMethod.getInstance()
                        ivEyeAgain.setImageResource(R.drawable.icon_login_eyes_n)
                    } else {
                        etPasswordAgain.transformationMethod =
                            HideReturnsTransformationMethod.getInstance()
                        ivEyeAgain.setImageResource(R.drawable.icon_login_eyes)
                    }
                }
                btnNext -> {
                    if (etPassword.text.isEmpty()) {
                        XToast.normal(getString(R.string.enter_password))
                        return@setOnclickNoRepeat
                    }
                    if (etPasswordAgain.text.isEmpty()) {
                        XToast.normal(getString(R.string.password_again))
                        return@setOnclickNoRepeat
                    }
                    if (!StringUtil.judgePassword(etPassword.text.toString()) ||
                        !StringUtil.judgePassword(etPasswordAgain.text.toString())
                    ) {
                        XToast.normal(getString(R.string.password_dialog_tip))
                        return@setOnclickNoRepeat
                    }
                    if (etPassword.text.toString() != etPasswordAgain.text.toString()) {
                        XToast.normal(getString(R.string.twice_password_not_same))
                        return@setOnclickNoRepeat
                    }
                    PersonalInformationActivity.openActivity(
                        mContext,
                        countryCode,
                        phone,
                        etPassword.text.toString()
                    )
                }
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, countryCode: String, phone: String) {
            val bundle = Bundle()
            bundle.putString("phone", phone)
            bundle.putString("countryCode", countryCode)
            mContext.startActivity(bundle, SetPasswordActivity::class.java)
        }
    }
}
